package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<String>add(@RequestBody ShoppingCart shoppingCart){
        //判断购物车是否存在菜品或者套餐
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        //判断条件：user_id 必须有，dish_id、setmeal_id 只能有一个
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        //将符合条件的加入购物车
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        //如果购物车为空，执行添加操作
        if(cart==null){
            //2.1 添加操作，往shopping——cart表中添加一条数据
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        }else {
            //如果不为空，执行更新操作 将数据做++操作
            //2.2 更新操作，将number++
             cart.setNumber(cart.getNumber()+1);
             shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");

    }

    /**
     * 查询购物车
     * @return
     */

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping ("/clean")
    public R<String> clean(){
        //QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        //queryWrapper.eq("user_id", BaseContext.getCurrentId());
        shoppingCartService.removeById( BaseContext.getCurrentId());
        return R.success("清空成功");
    }

    /**
     * 删除购物车
     */
    @PostMapping("/sub")
    public R<String> delete(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if(cart.getNumber()>1){
            cart.setNumber(cart.getNumber()-1);
            shoppingCartService.updateById(cart);
        }else {
            shoppingCartService.removeById(cart);
        }
        return R.success("删除一个成功");
    }



}